package org.grow.stable.security;


import org.grow.stable.security.dao.UserDao;
import org.grow.stable.security.domain.Role;
import org.grow.stable.security.domain.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
public class AuthController {
    @Resource
    private UserDao userDao;
    @GetMapping("/reg/{username}")
    public Boolean isExist(@PathVariable String username){
        if (userDao.findByUsername(username)!=null){
            return true;
        }
        return false;
    }
    @PutMapping("/reg")
    public String regUser(@RequestBody User user){
        //todo:
        user.setRoles(Arrays.asList(Role.THIRD,Role.FIFTH));
        user.setBatch(4);
        userDao.save(user);
        return user.getUsername();
    }

    @GetMapping("/user/{username}")
    public User getOne(@PathVariable String username){
        return userDao.findByUsername(username);

    }
}
